package com.hu.zhcc.shiro.controller.xsf;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.xsf.dto.THealthPlanDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TMealsDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.THealthPlan;
import com.hu.zhcc.shiro.entity.xsf.pojo.TMeals;
import com.hu.zhcc.shiro.entity.xsf.vo.THealthPlanVO;
import com.hu.zhcc.shiro.entity.xsf.vo.TMealsVO;
import com.hu.zhcc.shiro.service.xsf.THealthPlanService;
import com.hu.zhcc.shiro.service.xsf.TMealsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/tMeals")
public class TMealsController {
    @Autowired
    private TMealsService tMealsService;

    @GetMapping("")
    public ResponseEntity<Page<TMealsVO>> getRoleList(@RequestParam(required = false) String name,
                                                      int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TMealsDTO> result = this.tMealsService.queryAllTMeals(paremeters, offset, limit);
        List<TMealsVO> voList = new ArrayList<TMealsVO>();
        if( result.getRows()!=null ){

            for(TMealsDTO dto : result.getRows()) {
                TMealsVO vo = new TMealsVO();
                BeanUtils.copyProperties(dto, vo);
                voList.add(vo);
            }
//            System.out.println(voList);
            return ResponseEntity.ok(new Page<TMealsVO>(result.getTotal(), voList));
        }else {
            return null;
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<TMealsVO> getById(@PathVariable("id") int id) {
        TMealsDTO dto = this.tMealsService.getById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TMealsVO vo = new TMealsVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTEvaluate(@PathVariable("id") int id, @RequestBody TMeals vo) {
//        System.out.println(vo);
        int rows = this.tMealsService.updateTMeals(vo);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTEvaluate(@PathVariable("id") int id) {
        return this.tMealsService.removeTMeals(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("")
    public ResponseEntity<TMealsVO> saveTEvaluate(@RequestBody TMealsVO vo) {
        TMealsDTO dto = new TMealsDTO();
        BeanUtils.copyProperties(vo, dto);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dto.setCreateTime(simpleDateFormat.format(new Date()));
        this.tMealsService.saveTMeals(dto);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }
}
